package management.system.view.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import management.system.model.bean.UsuarioDTO;
import management.system.model.services.UsuarioService;
import management.system.utils.Constants;

/**
 * Servlet implementation class ServletLogueo
 */
@WebServlet("/login")
public class ServletLogueo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletLogueo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		UsuarioService usuarioService = new UsuarioService();
		UsuarioDTO objUsuario = null;
		String email = request.getParameter("txfEmail");
		String clave = request.getParameter("txtClave");
		
		try {
			objUsuario = usuarioService.getValidarUsuario(email, clave);
			Constants.usuarioDTO = objUsuario;
			if(objUsuario == null){
				response.sendRedirect("login.jsp");
			}else{
				HttpSession session = request.getSession();
				session.setAttribute("usuario_logueado", objUsuario);
				response.sendRedirect("jsp/principal.jsp");
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
